//
//  YNetworkRequest.h
//  YNetworking
//
//  Created by osnail on 2018/9/27.
//  Copyright © 2018 PH. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "YNetworkHelper.h"

@protocol YNetworkRequestDelegate <NSObject>

@optional
/// 请求结束 可以用来处理异常情况
/// @param responseObject 请求结果
- (void)requestCompletion:(id)responseObject;

@end

//请求缓存策略
typedef NS_ENUM(NSInteger,YNetworkRequest_cacheMode)
{
    YNetworkRequest_cacheMode_noCache, /// 不缓存
    YNetworkRequest_cacheMode_onFail,  /// 请求失败之后缓存
    YNetworkRequest_cacheMode_cacheAndRequest /// 先取缓存 然后拿请求数据
};

extern NSString *const kLoginStatusTokenOverdueNotification;

@interface YNetworkRequest : NSObject
<
YNetworkRequestDelegate
>

/// 请求类型 required overload default is GET
@property (nonatomic, assign) YRequestType requestType;
/// 请求地址 required overloadretryTimes
@property (nonatomic, copy) NSString *requestURL;
/// 域名 required overload
@property (nonatomic, copy) NSString *baseURL;
/// 参数 required overload
@property (nonatomic, strong) NSDictionary *requestParames;
/// 请求头 required overload
@property (nonatomic, strong) NSDictionary *requestHeaderField;
/// 请求使用json格式 default is JSON
@property (nonatomic, assign) BOOL requestSerializerJSON;
/// 返回jsonh格式  default is JSON
@property (nonatomic, assign) BOOL responseSerializerJSON;
/// 请求超时时间 release default is 30
@property (nonatomic, assign) NSTimeInterval timeoutTime;
/// DEBUG 状态下是否显示打印
@property (nonatomic, assign) BOOL showLog;
/// 缓存策略 默认不添加缓存
@property (nonatomic, assign) YNetworkRequest_cacheMode cacheMode;
/// 请求任务
@property (nonatomic, strong) NSURLSessionTask *requestTask;

/// 子类化 请求方式 建议使用
/// 需要重载属性
/// @param completion 请求结果回调
/// @param failure  请求失败错误回调
- (NSURLSessionTask *)startRequestWithCompletion:(void(^)(id responseObject))completion failure:(void(^)(NSError *error))failure;

/**
 请求方法
 
 @param method 请求方式
 @param url 请求的url
 @param parameters 请求参数
 @param handlerBlock 请求的回调
 */
- (NSURLSessionTask *)requestTypeMethod:(YRequestType )method
                                    url:(NSString *)url
                             parameters:(id)parameters
                                headers:(NSDictionary *)requestHeaderField
                                handler:(YHandlerBlock)handlerBlock;


/// 请求方法
/// @param method 请求方式
/// @param url 请求的url
/// @param parameters 请求参数
/// @param requestHeaderField 请求头部
/// @param cacheMode 缓存策略
/// @param handlerBlock 请求回调
- (NSURLSessionTask *)requestTypeMethod:(YRequestType)method
                                    url:(NSString *)url
                             parameters:(id)parameters
                                headers:(NSDictionary *)requestHeaderField
                              cacheMode:(YNetworkRequest_cacheMode)cacheMode
                                handler:(YHandlerBlock)handlerBlock;

/// 取消请求任务
- (void)cancleTask;
/// 请求是否已经取消
- (BOOL)isCancelled;
/// 请求是否正在执行
- (BOOL)isExecuting;

@end


